

<template>
<div style="width: 100vw;height: 100vh;position: relative">
  <div class="container login-container" style="position: absolute;left: calc(50% - 240px);top: 20%">
    <h1>登录</h1>
    <div class="input-group">
      <label for="login-email">用户名</label>
      <input type="email" id="login-email" placeholder="请输入您的用户名" v-model="loginForm.username">
    </div>
    <div class="input-group">
      <label for="login-password">密码</label>
      <input type="password" id="login-password" placeholder="请输入您的密码" v-model="loginForm.password">
    </div>
    <button class="btn" @click="login">登录</button>
    <a class="toggle-form" @click="toggleForm">没有账号？注册</a>
  </div>

  <div class="container register-container" style="position: absolute;left: calc(50% - 240px);top: 20%">
    <h1>注册</h1>
    <div class="input-group">
      <label for="register-email">用户名</label>
      <input type="email" id="register-email" placeholder="请输入您的用户名" v-model="registerForm.username">
    </div>
    <div class="input-group">
      <label for="register-email">手机号</label>
      <input type="email" id="register-email" placeholder="请输入您的手机号" v-model="registerForm.telephone">
    </div>
    <div class="input-group">
      <label for="register-password">密码</label>
      <input type="password" id="register-password" placeholder="请输入您的密码">
    </div>
    <div class="input-group">
      <label for="register-confirm-password">确认密码</label>
      <input type="password" id="register-confirm-password" placeholder="请确认您的密码" v-model="registerForm.password">
    </div>
    <button class="btn" @click="regist">注册</button>
    <a class="toggle-form" @click="toggleForm">已有账号？登录</a>
  </div>
</div>
</template>
<script>
export default {
  name: "LoginView",
  data(){
    return {
      loginForm:{
        username: '',
        password: ''
      },
      registerForm: {
        username: '',
        password: '',
        telephone: ''
      }
    }
  },
  methods:{
    toggleForm() {
      var loginContainer = document.querySelector('.login-container');
      var registerContainer = document.querySelector('.register-container');
      if (loginContainer.style.display === 'none') {
        loginContainer.style.display = 'block';
        registerContainer.style.display = 'none';
      } else {
        loginContainer.style.display = 'none';
        registerContainer.style.display = 'block';
      }
    },
    login(){
      this.$axios.post(
          "/sso/login",
          null,
          {
            params: this.loginForm
          }
      ).then(
          (resp) => {

            if (resp.data.code == 200) {
              alert("登录成功")
              localStorage.setItem("token", resp.data.data.token)
            }else{
              alert("登录失败，" + resp.data.message)
            }
          }
      ).catch(
          () =>{
            console.log('获取失败')
      }
      )
    },
    regist(){
      this.$axios.post(
          "/sso/register",
          null,
          {
            params: this.registerForm
          }
      ).then(
          (res) => {
            if (res.data.code == 200) {
              alert("注册成功")
              this.toggleForm()
            }else{
              alert("注册失败，" + res.data.message)
            }
          }
      ).catch(
          () => {
            console.log('出错')
          }
      )
    }
  }
}
</script>
<style scoped>

.container {
  background: #fff;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
  max-width: 400px;
  width: 100vw;
  transition: transform 0.6s ease-in-out, opacity 0.6s ease-in-out;
}

h1 {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
}

.input-group {
  margin-bottom: 20px;
  text-align: left;
}

.input-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #555;
}

.input-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.input-group input:focus {
  outline: none;
  border-color: #87ceeb;
}

.btn {
  width: 100%;
  padding: 15px;
  background: #87ceeb;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
  transition: background 0.3s, transform 0.3s;
}

.btn:hover {
  background: #4682b4;
  transform: translateY(-2px);
}

.forgot-password,
.toggle-form {
  margin-top: 10px;
  display: block;
  color: #87ceeb;
  text-decoration: none;
  cursor: pointer;
}

.forgot-password:hover,
.toggle-form:hover {
  text-decoration: underline;
}

.register-container {
  display: none;
}

@media (max-width: 600px) {
  .container {
    padding: 20px;
  }

  .btn {
    padding: 10px;
  }
}

@keyframes slideIn {
  from {
    transform: translateY(100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.container {
  animation: slideIn 0.6s ease-in-out;
}
</style>